package crew.blt.handsound.server.services;

import java.util.ArrayList;
import java.util.Collection;

import org.gwtwidgets.server.spring.GWTSpringController;
import org.springframework.security.core.context.SecurityContextHolder;

import crew.blt.handsound.client.services.user.UserService;
import crew.blt.handsound.server.dao.UserDAO;
import crew.blt.handsound.server.users.GaeUser;
import crew.blt.handsound.shared.domain.User;
import crew.gwt.client.domain.OrderedEntitiesResponse;

public class UserServiceImpl extends GWTSpringController implements UserService{

	private UserDAO userDao;
	
	@Override
	public User login() throws Exception {
		GaeUser gUser = (GaeUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return userDao.findUser(gUser.getUserId());
	}

	@Override
	public Collection<User> getUsers() {
		try{
			return userDao.getAll();
		}catch(Exception e){
			System.out.println("Erreur dans UserServiceImpl : getUsers() \n" + e);
			return null;
		}
	}
	
	@Override
	public User getUser(final String userId)throws Exception {
		try{
			return userDao.findUser(userId);
		}catch(Exception e){
			throw e;
		}
	}

	@Override
	public Boolean deleteUser(String id) throws Exception {
		throw new UnsupportedOperationException("Method to implement");
	}

	@Override
	public Boolean editUser(User u) throws Exception {
		throw new UnsupportedOperationException("Method to implement");
	}

	@Override
	public OrderedEntitiesResponse<User> getOrderedEntities(int index,boolean asc, int gridCapacity, int startIndex) {
		final Collection<User> entities = userDao.getAll();
		return new OrderedEntitiesResponse<User>(new ArrayList<User>(entities), entities.size());
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}
}
